package com.example.node.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.node.R;
import com.example.node.bean.MusicListBean;
import com.example.node.listener.OnClick;
import com.example.node.utils.GlideUtils;

import java.util.List;

public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.ViewHolder> {

    private List<MusicListBean> mList;
    private Context mContext;
    private OnClick click;

    public MusicListAdapter(List<MusicListBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void setOnClick(OnClick click) {
        this.click = click;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mContext=viewGroup.getContext();
        View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_music_list_item,viewGroup,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        MusicListBean musicListBean=mList.get(i);
        GlideUtils.loadImageView(mContext,musicListBean.getUrl(),viewHolder.iv_img);
        viewHolder.tv_text.setText(musicListBean.getTitle());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click.onClick(i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_img;
        private TextView tv_text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_img=itemView.findViewById(R.id.iv_img);
            tv_text=itemView.findViewById(R.id.tv_text);
        }
    }
}
